package com.xuxueli.crawler.test;

import com.xuxueli.crawler.XxlCrawler;
import com.xuxueli.crawler.annotation.PageFieldSelect;
import com.xuxueli.crawler.annotation.PageSelect;
import com.xuxueli.crawler.conf.XxlCrawlerConf;
import com.xuxueli.crawler.parser.PageParser;
import com.xuxueli.crawler.proxy.ProxyMaker;
import com.xuxueli.crawler.proxy.strategy.RoundProxyMaker;
import com.xuxueli.crawler.test.entity.Category;
import com.xuxueli.crawler.test.util.HibernateUtil;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;

import java.net.InetSocketAddress;
import java.net.Proxy;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/*
 *  lcx 爬取所有商品的url
 * */
public class Allcart {

    @PageSelect(cssQuery = "body")
    public static class PageVo {

        @PageFieldSelect(cssQuery = "h6.itemTitle", selectType = XxlCrawlerConf.SelectType.TEXT)
        private List<String> shopNameList;

        @PageFieldSelect(cssQuery = "a.itemLink.block", selectType = XxlCrawlerConf.SelectType.ATTR, selectVal = "href")
        private List<String> shopNameListHref;

        @PageFieldSelect(cssQuery = "span.itemPrice", selectType = XxlCrawlerConf.SelectType.TEXT)
        private List<String> shopPriceListHref;

        @PageFieldSelect(cssQuery = ".warning > .clearMargin-bottom > li", selectType = XxlCrawlerConf.SelectType.TEXT)
        private String endState;

        @PageFieldSelect(cssQuery = "a.img-link", selectType = XxlCrawlerConf.SelectType.ATTR, selectVal = "data-ean")
        private List<String> eanList1;

        @PageFieldSelect(cssQuery = "div.column-block", selectType = XxlCrawlerConf.SelectType.ATTR, selectVal = "data-ean")
        private List<String> eanList2;

        public void setEndState(String endState) {
            this.endState = endState;
        }

        public List<String> getEanList1() {
            return eanList1;
        }

        public void setEanList1(List<String> eanList1) {
            this.eanList1 = eanList1;
        }

        public List<String> getEanList2() {
            return eanList2;
        }

        public void setEanList2(List<String> eanList2) {
            this.eanList2 = eanList2;
        }

        public String getEndState() {
            return endState;
        }

        public List<String> getShopPriceListHref() {
            return shopPriceListHref;
        }

        public void setShopPriceListHref(List<String> shopPriceListHref) {
            this.shopPriceListHref = shopPriceListHref;
        }

        public List<String> getShopNameList() {
            return shopNameList;
        }

        public void setShopNameList(List<String> shopNameList) {
            this.shopNameList = shopNameList;
        }

        public List<String> getShopNameListHref() {
            return shopNameListHref;
        }

        public void setShopNameListHref(List<String> shopNameListHref) {
            this.shopNameListHref = shopNameListHref;
        }

        @Override
        public String toString() {
            return "PageVo{" +
                    "shopNameList='" + shopNameList + '\'' +
                    ", shopNameListHref=" + shopNameListHref + '}';
        }
    }

    public static void main(String[] args) {
        int i = 0;

        Map headerMap = new HashMap();
        Map cookieMap = new HashMap();
        cookieMap.put("BUYER_CITY_SELECTED","80");

        headerMap.put("Cache-Control", "max-age=0");

        String[] ip = {
                "222.85.39.159:38939",
                "49.85.5.17:28885"
        };

        // 设置代理池
        ProxyMaker proxyMaker = new RoundProxyMaker()
                .addProxy(new Proxy(Proxy.Type.HTTP, new InetSocketAddress("183.163.40.223", 31773)));

        //查询需要爬取得类目
        HibernateUtil hibernateUtil = new HibernateUtil();
        List<Category> categoriesList = hibernateUtil.selectAllCartByCategories();
        final List<PageVo> pageVoList = new ArrayList<>();


        for (Category categories : categoriesList) {

            final String[] link = new String[500];

            //如果是垃圾数据则跳过
            if ("null".equals(categories.getHref())) {
                continue;
            }
            for (int j = 1; j <= categories.getMaxpage(); j++) {
                link[j] = categories.getHref().replace("?ref=nav", "") + "&section=2&page=" + j;
            }
            //查询一个类目下的所有页商品
            System.out.printf("开始爬取第：" + i++ + "个类目的数据");
            XxlCrawler crawler = new XxlCrawler.Builder()
                    .setUrls(link)
                    .setThreadCount(1).setPauseMillis(1000)
                    .setAllowSpread(false)
                    //.setProxyMaker(proxyMaker)
                    .setCookieMap(cookieMap)
                    .setHeaderMap(headerMap)
                    .setPageParser(
                            new PageParser<PageVo>() {
                                HibernateUtil hibernateUtil = new HibernateUtil();

                                @Override
                                public boolean parse(Document html, Element pageVoElement, PageVo pageVo) {
                                    // 解析封装 PageVo 对象
                                    System.out.println("----------------------------------------------------------");
                                    System.out.println("------" + pageVo.toString());
                                    pageVoList.add(pageVo);
                                    System.out.println("---------------------------------------------------------");
                                    //hibernateUtil.saveCart(pageVo);
                                    //分页结束标志
                                    if (pageVo.getEndState() != null) {
                                        return false;
                                    }

                                    return true;
                                }
                            }
                    )
                    .build();
            System.out.println("start");
            crawler.start(true);
            System.out.println("end");

            if (!crawler.isRunState()) {
                System.out.println("-----------------这个类别已结束-------------------------");
                break;
            }
        }

        for (PageVo pageVo : pageVoList) {
            hibernateUtil.saveCart(pageVo);
        }
    }

}
